package com.miandian.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miandian.common.R;
import com.miandian.entities.Menu;
import com.miandian.service.MenuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
@Api(value = "菜单管理",tags = "菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @GetMapping("/getList")
    public R menu(HttpServletRequest request){
        Page pageInfo = new Page(1,100);
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(request.getParameter("name")),Menu::getName,request.getParameter("name"));
//        queryWrapper.eq(StringUtils.isNotBlank(request.getParameter("name")),Menu::getName,menu.getName());
        menuService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Menu menu){
        menu.setStatus(1);
        menuService.save(menu);
        return R.success("保存成功");
    }

    @DeleteMapping("/delete")
    public R deleteById(@RequestParam List<Integer> ids){
        System.out.println(ids);
        menuService.removeByIds(ids);
//        JSONArray jsonArray = JSON.parseArray(ids);
//        System.out.println(jsonArray);
//        List<Integer> list = jsonArray.toJavaList(Integer.class);
        return R.success("删除成功");
    }
    /**
     * 菜单详情
     */
    @GetMapping("/detail")
    public R detail(@RequestParam Integer id){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Menu menu = menuService.getOne(queryWrapper);
        return R.success(menu);
    }
    /**
     * 修改菜单
     */
    @PutMapping("/update")
    public R update(@RequestBody Menu menu){
        menuService.updateById(menu);
        return R.success("修改成功");
    }
}
